package com.hksj.em.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hksj.common.core.model.Rest;
import com.hksj.em.dto.create.CreateIntelligentDispatchingDTO;
import com.hksj.em.entity.IntelligentDispatching;
import com.hksj.em.mapper.IntelligentDispatchingMapper;
import com.hksj.em.query.IntelligentDispatchingQuery;
import com.hksj.em.vo.DevicesVO;
import com.hksj.em.vo.IntelligentDispatchingVO;
import com.hksj.em.vo.MaxAndMinTimeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class IntelligentDispatchingService {

    @Autowired
    private IntelligentDispatchingMapper mapper;

    public List<IntelligentDispatchingVO> queryIntelligentDispatchingRecord(IntelligentDispatchingQuery query) {
        // 查询当天所有手术
        List<IntelligentDispatchingVO> list = mapper.queryIntelligentDispatchingRecord(query);
        // 检查 list 是否为空
        if (list == null || list.isEmpty()) {
            return Collections.emptyList();
        }
        // 提取 list 中的所有 roomCode 并去重
        List<String> uniqueRoomCode = list.stream()
                .map(IntelligentDispatchingVO::getRoomCode)
                .distinct()
                .collect(Collectors.toList());
        // 通过 roomCode 查询首台开始时间、末台结束时间
        List<MaxAndMinTimeVo> list1 = mapper.selectMaxAndMintimeByRoomCodes(uniqueRoomCode);
        // 检查 list1 是否为空
        if (list1 == null || list1.isEmpty()) {
            return list; // 直接返回原始数据
        }
        // 将 list1 转换为以 roomCode 为键的 Map，提高查找效率
        Map<String, MaxAndMinTimeVo> roomCodeToMaxMinTimeMap = list1.stream()
                .collect(Collectors.toMap(MaxAndMinTimeVo::getRoomCode, vo -> vo));
        // 遍历 list 并设置首台开始时间和末台结束时间
        list.forEach(vo -> {
            MaxAndMinTimeVo maxAndMinTimeVo = roomCodeToMaxMinTimeMap.get(vo.getRoomCode());
            if (maxAndMinTimeVo != null) {
                vo.setFirstOneStartTime(maxAndMinTimeVo.getFirstOneStartTime());
                vo.setFirstOneEndTime(maxAndMinTimeVo.getFirstOneEndTime());
            }
        });
        return list;
    }

    public Rest insertOrUpdateById(CreateIntelligentDispatchingDTO dto) {
        if (dto == null) {
            return Rest.fail("参数不能为空");
        }
        IntelligentDispatching entity = new IntelligentDispatching();
        BeanUtils.copyProperties(dto, entity);
        IntelligentDispatching intelligentDispatching = mapper.selectById(entity.getId());
        if (intelligentDispatching == null) {
            mapper.insert(entity);
        } else {
            int i = mapper.updateById(entity);
            if (i <= 0) {
                return Rest.fail("更新失败，记录不存在");
            }
        }
        return Rest.ok();
    }
}
